package cn.shujuhai.qtadmin.platform.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jetbrains.annotations.NotNull;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.hutool.captcha.CaptchaUtil;
import cn.shujuhai.qtadmin.platform.kit.Constant;

/**
 * 用户API
 *
 * @author dch798
 * @date 2021/08/21 22:13
 **/
@RestController
@RequestMapping("Captcha")
public class CaptchaController {

    /**
     * @param session
     *            session
     * @param response
     *            response
     */
    @GetMapping("getCaptcha")
    public void getCaptcha(@NotNull HttpSession session, @NotNull HttpServletResponse response) throws IOException {
        var gifCaptcha = CaptchaUtil.createGifCaptcha(200, 100);
        session.setAttribute(Constant.CAPTCHA_SESSION_KEY, gifCaptcha.getCode());
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-store,no-cache,must-revalidate");
        response.setHeader("Cache-Control", "post-check=0,pre-check=0");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/gif");
        var out = response.getOutputStream();
        gifCaptcha.write(out);
        out.flush();
        out.close();
    }
}
